//
//  XHTableViewController.m
//  TwitterPagging
//
//  Created by 曾 宪华 on 14-6-21.
//  Copyright (c) 2014年 嗨，我是曾宪华(@xhzengAIB)，曾加入YY Inc.担任高级移动开发工程师，拍立秀App联合创始人，热衷于简洁、而富有理性的事物 QQ:543413507 主页:http://zengxianhua.com All rights reserved.
//

#import "XHTableViewController.h"
#import "JonesSingle.h"
#import "JPUSHService.h"
#import "HomeNewsCell.h"
#import "NewsModel.h"
#import "NewsAction.h"
#import "SideNewsViewModel.h"

@interface XHTableViewController ()<GetHomeSideNewsDelegate>{
    NSInteger ChooseType;
    NSMutableArray *topArray;
    NSInteger   page;
    
    NSInteger zan_cun_change;
    
    NSInteger cun_int ;
}

@property (nonatomic, strong) NSString *key_city;
@property (nonatomic, strong) NSString *key_car;

@end

@implementation XHTableViewController

- (void)loadDataSource {
    self.dataSource = [NSMutableArray new];
    topArray = [NSMutableArray new];
    ChooseType = 1;
    if (_type==1) {
        ChooseType = 2;
    }
}

#pragma mark - DZNEmptyDataSetDelegate
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    [self.tableView.mj_header beginRefreshing];
}

-(void)GetActivitySucces:(int)value array:(NSMutableArray *)array page:(NSInteger)page{
    if (value == 1) {
        if (page==0) {
            [self.dataSource removeAllObjects];
        }
        [self.dataSource addObjectsFromArray:array];
        
        if (array.count<15) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.tableView.mj_footer.hidden = NO;
        }
    }
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    cun_int ++;
    
    if (_type!=100) {
        if (cun_int>1) {
            if (self.dataSource.count<1) {
                [self.tableView.mj_header beginRefreshing];
            }
        }
    }else{
        if (self.dataSource.count<1) {
            [self.tableView.mj_header beginRefreshing];
        }
    }

//    BOOL lin_load_ok = NO;
//    if (_type==1) {
//
//    }else if (_type==2){
//
//    }
//    if (lin_load_ok) {
//        [self.tableView.mj_header beginRefreshing];
//    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadDataSource];
    MJHiCarHeader *header           = [MJHiCarHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadnew)];
    header.automaticallyChangeAlpha    = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden           = YES;
    self.tableView.mj_header          = header;
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
//    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer           = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadmore];
    }];
    self.tableView.mj_footer.hidden    = YES;
}

#pragma mark - DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"error"];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    NSString *text;
    NSMutableAttributedString *attStr;
    text = @"无数据?点击重新加载";
    attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:NSFontAttributeName
                   value:[UIFont systemFontOfSize:14.0]
                   range:NSMakeRange(0, text.length)];
    [attStr addAttribute:NSForegroundColorAttributeName
                   value:[JonesClass stringTOColor:@"#cccccc"]
                   range:NSMakeRange(0, text.length)];
    return attStr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)loadmore{
    NewsModel *modellin = [self.dataSource lastObject];
    page = [modellin.news_id integerValue];
    
    NSMutableDictionary *postdic = [NSMutableDictionary new];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)page] forKey:@"page"];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)_type] forKey:@"type"];
    SideNewsViewModel *viewmodel = [[SideNewsViewModel alloc] init:postdic];
    viewmodel.delegate = self;
}

- (void)loadnew{
    page = 0;

    NSMutableDictionary *postdic = [NSMutableDictionary new];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)page] forKey:@"page"];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)_type] forKey:@"type"];
    SideNewsViewModel *viewmodel = [[SideNewsViewModel alloc] init:postdic];
    viewmodel.delegate = self;
}

- (void)GetHomeSideNewsSucces:(int)value array:(NSMutableArray *)array page:(NSInteger)page{
    if (value == 1) {
        if (page==0) {
            [self.dataSource removeAllObjects];
        }
        [self.dataSource addObjectsFromArray:array];
        
        if (array.count<15) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.tableView.mj_footer.hidden = NO;
        }
    }
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

#pragma mark - UITableView DataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeNewsCell *cell = [HomeNewsCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
    cell.Model = [self.dataSource objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - UITableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsModel *model = [self.dataSource objectAtIndex:indexPath.row];
    return model.news_height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsModel *model = [self.dataSource objectAtIndex:indexPath.row];
    [NewsAction Actiontodone:JonesGOTO_NewsInfo view:self dictionary:@{@"model":model}];
    model.IsRead = YES;
    [tableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
